package com.castle.fortress.admin.study.exam.service.impl;

import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.castle.fortress.admin.study.exam.dto.StudyExamDto;
import com.castle.fortress.admin.study.exam.entity.StudyExamEntity;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.exam.entity.StudyExamStatisticsEntity;
import com.castle.fortress.admin.study.exam.dto.StudyExamStatisticsDto;
import com.castle.fortress.admin.study.exam.mapper.StudyExamStatisticsMapper;
import com.castle.fortress.admin.study.exam.service.StudyExamStatisticsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 试卷统计 服务实现类
 *
 * @author castle
 * @since 2023-02-16
 */
@Service
public class StudyExamStatisticsServiceImpl extends ServiceImpl<StudyExamStatisticsMapper, StudyExamStatisticsEntity> implements StudyExamStatisticsService {
    /**
    * 获取查询条件
    * @param studyExamStatisticsDto
    * @return
    */
    public QueryWrapper<StudyExamStatisticsEntity> getWrapper(StudyExamStatisticsDto studyExamStatisticsDto){
        QueryWrapper<StudyExamStatisticsEntity> wrapper= new QueryWrapper();
        if(studyExamStatisticsDto != null){
            StudyExamStatisticsEntity studyExamStatisticsEntity = ConvertUtil.transformObj(studyExamStatisticsDto,StudyExamStatisticsEntity.class);
            wrapper.eq(studyExamStatisticsEntity.getId() != null,"id",studyExamStatisticsEntity.getId());
            wrapper.eq(studyExamStatisticsEntity.getMemberId() != null,"member_id",studyExamStatisticsEntity.getMemberId());
            wrapper.eq(studyExamStatisticsEntity.getExamId() != null,"exam_id",studyExamStatisticsEntity.getExamId());
            wrapper.eq(studyExamStatisticsEntity.getRecordId() != null,"record_id",studyExamStatisticsEntity.getRecordId());
            wrapper.like(studyExamStatisticsEntity.getTimeConsuming() != null,"time_consuming",studyExamStatisticsEntity.getTimeConsuming());
            wrapper.like(studyExamStatisticsEntity.getQuizTime() != null,"quiz_time",studyExamStatisticsEntity.getQuizTime());
            wrapper.like(studyExamStatisticsEntity.getHighScore() != null,"high_score",studyExamStatisticsEntity.getHighScore());
            wrapper.eq(studyExamStatisticsEntity.getExamNum() != null,"exam_num",studyExamStatisticsEntity.getExamNum());
        }
        return wrapper;
    }


    @Override
    public IPage<StudyExamStatisticsDto> pageStudyExamStatistics(Page<StudyExamStatisticsDto> page, StudyExamStatisticsDto studyExamStatisticsDto) {
        QueryWrapper<StudyExamStatisticsEntity> wrapper = getWrapper(studyExamStatisticsDto);
        Page<StudyExamStatisticsEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyExamStatisticsEntity> studyExamStatisticsPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyExamStatisticsDto> pageDto = new Page(studyExamStatisticsPage.getCurrent(), studyExamStatisticsPage.getSize(),studyExamStatisticsPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyExamStatisticsPage.getRecords(),StudyExamStatisticsDto.class));
        return pageDto;
    }


    @Override
    public List<StudyExamStatisticsDto> listStudyExamStatistics(StudyExamStatisticsDto studyExamStatisticsDto){
        QueryWrapper<StudyExamStatisticsEntity> wrapper = getWrapper(studyExamStatisticsDto);
        List<StudyExamStatisticsEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyExamStatisticsDto.class);
    }

    @Override
    public StudyExamStatisticsDto queryByMemberAndExam(Long memberId, Long examId) {
        StudyExamStatisticsDto studyExamStatisticsDto = new StudyExamStatisticsDto();
        studyExamStatisticsDto.setMemberId(memberId);
        studyExamStatisticsDto.setExamId(examId);
        List<StudyExamStatisticsDto> list = listStudyExamStatistics(studyExamStatisticsDto);
        if(list ==null || list.isEmpty()){
            return null;
        }else if(list.size()>1){
            throw new BizException(GlobalRespCode.DB_DATA_ERROR);
        }
        return list.get(0);
    }

    @Override
    public IPage<StudyExamStatisticsDto> pageStudyExamStatisticsExtends(Page<StudyExamStatisticsDto> page, StudyExamStatisticsDto studyExamStatisticsDto) {
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyExamStatisticsEntity studyExamStatisticsEntity = ConvertUtil.transformObj(studyExamStatisticsDto,StudyExamStatisticsEntity.class);
        List<StudyExamStatisticsEntity> studyExamStatisticsEntities=baseMapper.extendsList(pageMap,studyExamStatisticsEntity);
        Long total = baseMapper.extendsCount(studyExamStatisticsEntity);
        Page<StudyExamStatisticsDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyExamStatisticsEntities,StudyExamStatisticsDto.class));
        return pageDto;
    }

    @Override
    public boolean updateByIsDelete(StudyExamStatisticsEntity statisticsEntity) {
        return baseMapper.updateByIsDelete(statisticsEntity);
    }

}

